using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace CollegeShop.Infrastructure.Entities.Repository
{
    [Table("T_Goods")]
    public class T_Goods : IEntity<T_Goods>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(64)]
        public string GoodsCode { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string GoodsName { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(100)]
        public string GoodsNamePY { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(512)]
        public string GoodsTitel { get; set; }

        public int GoodsCategoryID { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(32)]
        public string Standard { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(16)]
        public string Unit { get; set; }

        public int PackStandard { get; set; }

        [Column(TypeName="text")]
        public string GoodsDescription { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(256)]
        public string Style { get; set; }

        public decimal DefaultSalePrice { get; set; }

        public int State { get; set; }
        public int ResourceId { get; set; }
        public int SaleListId { get; set; }

        public override T_Goods Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.ID == id);
        }

        public override T_Goods Find(System.Linq.Expressions.Expression<Func<T_Goods, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Goods.FirstOrDefault(where);
                if (item != null)
                {
                    this.ID = item.ID;
                    this.GoodsCode = item.GoodsCode;
                    this.GoodsName = item.GoodsName;
                    this.GoodsNamePY = item.GoodsNamePY;
                    this.GoodsTitel = item.GoodsTitel;
                    this.GoodsCategoryID = item.GoodsCategoryID;
                    this.Standard = item.Standard;
                    this.Unit = item.Unit;
                    this.PackStandard = item.PackStandard;
                    this.GoodsDescription = item.GoodsDescription;
                    this.DefaultSalePrice = item.DefaultSalePrice;
                    this.State = item.State;
                    this.ResourceId = item.ResourceId;
                    this.Style = item.Style;
                    this.SaleListId = item.SaleListId;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Goods, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Goods.Any(where);
            }
        }
    }
}
